package com.hspedu.threaduse;

/**
 * @author LiuJinTao
 * @className Thread02
 * @date 2024/06/23 0:45
 * @description 通过实现接口 Runnable 开发线程
 */
public class Thread02 {
    public static void main(String[] args) {
//        Dog dog = new Dog();
////        dog.start();//这里不能调用 start
//        // 创建了 Thread 对象，把 Dog 对象（实现了 Runnable），放入 Thread
//        Thread thread = new Thread(dog);
//        thread.start();
        Tiger tiger = new Tiger();
        ThreadProxy threadProxy = new ThreadProxy(tiger);
        threadProxy.start();
    }
}

class Animal {}

class Tiger extends Animal implements Runnable{
    int count = 0;
    @Override
    public void run() {
        while (true) {
            System.out.println("老虎叫" + (++count) + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (count == 10) break;

        }
    }
}

class ThreadProxy implements Runnable {// 把 Proxy 类当做 ThreadProxy
    private Runnable target = null;// 属性。类型 Runnable

    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }

    public ThreadProxy(Runnable target) {
        this.target = target;
    }

    public void start() {
        start0();
    }

    public void start0() {
        run();
    }
}


class Dog implements Runnable {
    int count = 0;

    @Override
    public void run() {
        while (true) {
            System.out.println("小狗汪汪叫" + (++count) + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (count == 10) break;

        }
    }
}